package com.banciyuan.bcywebview.biz.main.mainpage.timeline.viewholder;

import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import de.greenrobot.daoexample.model.Timeline;

/**
 * Created by squall on 2017/6/22.
 */

public class GridWriterHolder extends RecyclerView.ViewHolder {

    private ImageView mCover;
    private TextView mTitle;
    private TextView mTags;

    public GridWriterHolder(View itemView) {
        super(itemView);
        mCover = (ImageView) itemView.findViewById(R.id.item_cover);
        mTitle = (TextView) itemView.findViewById(R.id.item_title);
        mTags = (TextView) itemView.findViewById(R.id.item_tag);
    }

    public void setGridWriter(GridWriterHolder gridWriterHolder, Timeline mItem, View.OnClickListener onClickListener) {
        if (!TextUtils.isEmpty(mItem.getPlain())) {
            gridWriterHolder.mTitle.setText(Html.fromHtml(mItem.getPlain()));
        }
        if (!TextUtils.isEmpty(mItem.getCover())) {
            gridWriterHolder.mCover.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage(mItem.getCover(), gridWriterHolder.mCover, ImageOptions.options);
        } else {
            gridWriterHolder.mCover.setVisibility(View.GONE);
        }
        String tagString = getTagString(mItem);
        if (TextUtils.isEmpty(tagString)) {
            gridWriterHolder.mTags.setVisibility(View.GONE);
        } else {
            gridWriterHolder.mTags.setVisibility(View.VISIBLE);
            gridWriterHolder.mTags.setText(tagString);
        }
        gridWriterHolder.itemView.setOnClickListener(onClickListener);
    }

    private String getTagString(Timeline mItem) {
        StringBuffer sb = new StringBuffer();
        if (null != mItem && null != mItem.getPost_tags()) {
            for (String s : mItem.getPost_tags()) {
                sb.append(s);
                sb.append("/");
            }
            if (sb.length() > 0)
                sb.delete(sb.length() - 1, sb.length());
        }
        return sb.toString();
    }
}
